package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * @author gengjian
     * @date 2024/12/16 00:28:26
     * 获取个人租约基本信息列表
     */
    @Override
    public List<AgreementItemVo> listItem(String phone) {
        List<AgreementItemVo> list = leaseAgreementMapper.listItem(phone);
        return list;
    }

    /**
     * @author gengjian
     * @date 2024/12/16 08:45:39
     * 根据id获取租约详细信息
     */
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        //根据租约id获取租约基本信息
        LeaseAgreement leaseAgreement = super.getById(id);
        //根据公寓id获取公寓名称
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        String apartmentName = apartmentInfo.getName();
        //根据公寓id和type获取公寓图片列表
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(
                ItemType.APARTMENT, leaseAgreement.getApartmentId());
        //根据房间id获取房间号
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        String roomNumber = roomInfo.getRoomNumber();
        //根据房间id和type获取房间图片列表
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(
                ItemType.ROOM, leaseAgreement.getRoomId());
        //根据支付方式id获取支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        String paymentTypeName = paymentType.getName();
        //根据租期id获取租期月数和单位
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        Integer leaseTermMonthCount = leaseTerm.getMonthCount();
        String leaseTermUnit = leaseTerm.getUnit();
        //创建AgreementDetailVo对象并封装
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermMonthCount);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setLeaseTermUnit(leaseTermUnit);
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        //返回
        return agreementDetailVo;
    }
}




